package club.yuit.desktop.ui;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author: yuit
 * @date: 2023/11/29 23:39
 */
@Slf4j
public class JTextPanelLinePointer  extends JPanel {

    public final static String SEND_POINTER_VALUE = "[MSG]";
    public final static String STATUS_POINTER_VALUE = "[RES]";
    public final static String SPLIT_POINTER_VALUE = "     |";
    public final static String SPLIT_END_POINTER_VALUE = "   |-----";



    // 用于生成ID
    private final AtomicInteger ID_GENERATOR = new AtomicInteger(0);

    private final Map<Integer,PointerGroup> groupMap;

    private PointerGroup currentGroup;
    public JTextPanelLinePointer() {
        this.setBackground(Color.WHITE);
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        groupMap = new LinkedHashMap<>();
        initFirstGroup(SEND_POINTER_VALUE,ColorUtil.RGB(255,128,0));
    }

    public void initFirstGroup(String value, Color color){
        int id  = ID_GENERATOR.incrementAndGet();
        JLabel label = newLabel(value, color);
        this.add(label,0);
        currentGroup = new PointerGroup(id);
        currentGroup.isFirst = true;
        currentGroup.addComponent(label);
        groupMap.put(id,currentGroup);
        if(currentGroup.getId()!=0){
            this.updateUI();
        }
    }





    @Override
    public Insets getInsets() {
        return new Insets(0,10,20,5);
    }

    public void addGroup(String value, Color color){
         int id  = ID_GENERATOR.incrementAndGet();
         currentGroup = new PointerGroup(id);
         groupMap.put(id,currentGroup);
         if(currentGroup.getId()!=0){
             this.updateUI();
         }
     }

     public boolean isFirstGroup(){
         return currentGroup.isFirst;
     }



    public void addGroup(String value,Color color, int index){
        int id  = ID_GENERATOR.incrementAndGet();
        currentGroup = new PointerGroup(id);
        groupMap.put(id,currentGroup);
        if(currentGroup.getId()!=0){
            this.updateUI();
        }
    }

     public int getBoxComponentCount(){
         return this.getComponentCount();
     }

     public void addCurrentGroupComponent(String value,int index){
            JLabel l = newLabel(value);
            if(index>=0){
                this.add(l,index);
            }else {
                this.add(l);
            }

            currentGroup.addComponent(l);
            this.updateUI();
     }




    /**
     * 添加组件到面板中(不覆盖添加的组件)
     * @param value
     * @param color
     * @param index 小于 0表示最后一位
     */
    public void addCurrentGroupComponent(String value,Color color,int index){

        if(index>=0 && index<currentGroup.components.size()){
            JComponent cp = currentGroup.components.get(index);
            if(cp!=null){
                return;
            }
        }

        JLabel l = newLabel(value,color);
        log.debug("add component pos: {}",index);
        if(index>=0){
            this.add(l,index);
        }else {
            this.add(l);
        }

        currentGroup.addComponent(l);
        this.updateUI();
    }


     public void removeCurrentGroupComponent(int index){
         if(index>=0 && index<currentGroup.components.size()){
            this.remove(currentGroup.components.get(index));
            currentGroup.components.remove(index);
            this.updateUI();
         }
     }

    private JLabel newLabel(String value){
        JLabel label = new JLabel(value);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setBorder(null);
        Font f = new Font("Dialog",Font.PLAIN,12);
        label.setFont(f);
        label.setVerticalAlignment(JLabel.CENTER);
        return label;
    }

    private JLabel newLabel(String value,Color color){
        JLabel label = newLabel(value);
        label.setForeground(color);
        return  label;
    }


    public void setFont(Font font){
        if(groupMap!=null){
            for(PointerGroup group : groupMap.values()){
                for(JComponent c : group.components){
                    c.setFont(font);
                }
            }
        }
        super.setFont(font);

    }



}


class PointerGroup {
    int id;
    boolean isFirst = false;
    List<JComponent> components;

    public PointerGroup(int id) {
        this.id = id;
        this.components = new ArrayList<>();
    }

    public boolean isFirst() {
        return isFirst;
    }

    public int getId() {
        return id;
    }

    public void addComponent(JComponent component){
        components.add(component);
    }

}